package com.mb.ext.web.controller.user;

import com.mb.ext.core.constant.AuthenticationConstants;
import com.mb.ext.core.model.ProductCateDTOResponse;
import com.mb.ext.core.model.ProductTypeResponse;
import com.mb.ext.core.service.ProductCateService;
import com.mb.ext.core.service.ProductService;
import com.mb.ext.core.service.spec.ResultDTO;
import com.mb.ext.core.service.spec.product.ProductDTO;
import com.mb.framework.exception.DAOException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.List;

/**
 * @Description
 * @Author 孙浩
 * @Version V1.0.0
 * @Date 2021/7/2 0002
 */
@RequestMapping("/user")
@Controller
public class HomePageController {

  @Autowired
  private ProductCateService productCateService;
  @Autowired
  private ProductService productService;

  /**
   * 查询类型
   */
   @GetMapping("/type_list")
   @ResponseBody
   @ResponseStatus(HttpStatus.CREATED)
   public ResultDTO typeList() throws DAOException {
     ResultDTO resultDTO = new ResultDTO();
     ProductCateDTOResponse productCateDTOResponse = new ProductCateDTOResponse();
     productCateDTOResponse.setProductCateDTOList(productCateService.typeList());
     resultDTO.getBody().setData( productCateDTOResponse);
     resultDTO.getBody().getStatus().setStatusCode(AuthenticationConstants.STATUS_CODE_SUCCESS);
     return resultDTO;
   }

  /**
   * 根据类型查询对应的商品
   */
  @GetMapping("/get_product_by_type")
  @ResponseBody
  @ResponseStatus(HttpStatus.CREATED)
  public ResultDTO getProductByType(@RequestParam(value = "typeId",defaultValue = "1") String typeId,
                                    @RequestParam(value = "startIndex",defaultValue = "10") Integer startIndex,
                                    @RequestParam(value = "pageSize",defaultValue = "1") Integer pageSize,
                                    @RequestParam(value = "search",required = false) String search
                                    ) throws DAOException {

    ResultDTO resultDTO = new ResultDTO();
    ProductTypeResponse productTypeResponse = new ProductTypeResponse();
    int total = productService.countByProductType(typeId,search);
    List<ProductDTO> list = productService.selectProductByType(typeId, startIndex, pageSize,search);
    productTypeResponse.setTotal(total);
    productTypeResponse.setProductDTOList(list);
    resultDTO.getBody().setData(productTypeResponse);
    resultDTO.getBody().getStatus().setStatusCode(AuthenticationConstants.STATUS_CODE_SUCCESS);
    return resultDTO;
  }
}
